package com.team.abs.service.baiduapi;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import com.team.abs.service.baidubag.Base64Util;
import com.team.abs.service.baidubag.FileUtil;
import com.team.abs.service.baidubag.GsonUtils;
import com.team.abs.service.baidubag.HttpUtil;

/**
* 人脸注册
*/
public class faceapi {

    //人脸添加
    public static String faceadd(String imgPath,String user_id,String user_info) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/add";
        try {
        	  byte[] bytes = FileUtil.readFileByBytes(imgPath);
              String image = Base64Util.encode(bytes);
            Map<String, Object> map = new HashMap<>();
            map.put("image", image);
            map.put("group_id", "group_repeat");
            map.put("user_id", user_id);
            map.put("user_info", user_info);
            map.put("liveness_control", "NORMAL");//活体检测控制
            map.put("image_type", "BASE64");
            map.put("quality_control", "NORMAL");//图片质量控制

            String param = GsonUtils.toJson(map);

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = accesstoken.getAuth();

            String result = HttpUtil.post(url, accessToken, "application/json", param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    //删除用户某张图片
    public static String facedelete(String face_token,String user_id,String group_id) {
        String url = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/face/delete";
        try {
            Map<String, Object> map = new HashMap<>();  
            map.put("group_id", "group_repeat");
            map.put("user_id", user_id);
            map.put("face_token", face_token);
            String param = GsonUtils.toJson(map);
            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = accesstoken.getAuth();

            String result = HttpUtil.post(url, accessToken, "application/json", param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    //查询用户facetoken
    public static String facesearchlist(String user_id,String group_id) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/face/getlist";
        try {
        	
            Map<String, Object> map = new HashMap<>();    
            map.put("group_id", "group_repeat");
            map.put("user_id", user_id);
            String param = GsonUtils.toJson(map);

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = accesstoken.getAuth();

            String result = HttpUtil.post(url, accessToken, "application/json", param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    //更新用户
    public static String faceUpdate(String imgPath,String user_id,String user_info) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/update";
        try {
        	  byte[] bytes = FileUtil.readFileByBytes(imgPath);
              String image = Base64Util.encode(bytes);
            Map<String, Object> map = new HashMap<>();
            map.put("image", image);
            map.put("group_id", "group_repeat");
            map.put("user_id", user_id);
            map.put("user_info", user_info);
            map.put("liveness_control", "NORMAL");
            map.put("image_type", "BASE64");
            map.put("quality_control", "LOW");

            String param = GsonUtils.toJson(map);

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken =accesstoken.getAuth();
            String result = HttpUtil.post(url, accessToken, "application/json", param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    //人脸比对查询
    public static String facesearch(String imgPath) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/face/v3/search";
        try {
        	byte[] bytes = FileUtil.readFileByBytes(imgPath);
            String image = Base64Util.encode(bytes);
            Map<String, Object> map = new HashMap<>();
            map.put("image", image);
            //是否活物
            map.put("liveness_control", "NONE");
            map.put("group_id_list", "group_repeat,group_233");
            //图片格式
            map.put("image_type", "BASE64");
            //图片质量
            map.put("quality_control", "LOW");

            String param = GsonUtils.toJson(map);

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = accesstoken.getAuth();

            String result = HttpUtil.post(url, accessToken, "application/json", param);
           
			 JSONObject jsonObject = new JSONObject(result);
			
			if(!jsonObject.get("error_msg").toString().equals("SUCCESS"))return null;
            String user_id = jsonObject.getJSONObject("result").getJSONArray("user_list").getJSONObject(0).getString("user_id");
            System.out.println(user_id);
            return user_id;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
    	//faceadd("D:\\img\\01.png","33102119990220277X","林昊学");
    	//facesearch("D:\\img\\02.png");
    	facesearchlist("33102119990220277X","");
    	//facedelete("ba06856ba4fd6700f5af38019ff5d0ea","33102119990220277X","group_repeat");
    }

}